package main

import (
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"log"
	"net/http"
	"strings"
)

// Logger 中间件记录有关每个 HTTP 请求的信息。
func main() {
	e := echo.New()

	//方式一：直接使用
	userGroup := e.Group("/default", middleware.Logger())
	userGroup.GET("/get", GetUserInfo)

	//方式二： 自定义config,
	carGroup := e.Group("/custom")
	carGroup.Use(middleware.LoggerWithConfig(CustomLoggerConfig()))
	carGroup.GET("/get", GetUserInfo)

	e.Start(":80")

}

func CustomLoggerConfig() middleware.LoggerConfig {
	l := middleware.LoggerConfig{
		Skipper: func(c echo.Context) bool {
			uri := c.Request().RequestURI
			if strings.Contains(uri, "/index") {
				log.Println("uri:", uri, "--->>>Skipper-true")
				return true
			}
			log.Println("uri:", uri, "--->>>Skipper-false")
			return false
		},
		Format: "method=${method}, uri=${uri}, status=${status}\n",
	}
	return l
}

func GetUserInfo(c echo.Context) error {
	user := User{
		UserId:   1,
		UserName: "admin",
		Password: "123456",
		Email:    "admin@qq.com",
	}
	return c.JSON(http.StatusOK, user)
}

type User struct {
	UserId   int    `json:"userid"`
	UserName string `json:"username"`
	Password string `json:"password"`
	Email    string `json:"email"`
}
